package com.tdam2011.grupo05.database.dao;

import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

import com.google.common.collect.Lists;
import com.tdam2011.grupo05.database.content.TdamContentProvider;
import com.tdam2011.grupo05.entities.account.Account;

public class AccountDao implements Dao<Account> {

	private static final String[] PROJECTION = { Account.COL_ID,
			Account.COL_USERNAME, Account.COL_PASSWORD, Account.COL_LAST_SYNC };

	private final ContentResolver cr;

	public AccountDao(Context context) {
		cr = context.getContentResolver();
	}

	@Override
	public long save(Account t) {
		final ContentValues cv = new ContentValues();
		cv.put(Account.COL_USERNAME, t.getUsername());
		cv.put(Account.COL_PASSWORD, t.getPassword());
		cv.put(Account.COL_LAST_SYNC, t.getLastSync());

		Uri u = cr.insert(TdamContentProvider.ACCOUNT_CONTENT_URI, cv);
		return ContentUris.parseId(u);
	}

	@Override
	public void update(Account t) {
		final ContentValues cv = new ContentValues();
		cv.put(Account.COL_USERNAME, t.getUsername());
		cv.put(Account.COL_PASSWORD, t.getPassword());
		cv.put(Account.COL_LAST_SYNC, t.getLastSync());
		cr.update(TdamContentProvider.ACCOUNT_CONTENT_URI, cv, BaseColumns._ID
				+ "= ?", new String[] { String.valueOf(t.getId()) });
	}

	@Override
	public void delete(Account t) {
		cr.delete(TdamContentProvider.ACCOUNT_CONTENT_URI, BaseColumns._ID
				+ "=?", new String[] { String.valueOf(t.getId()) });
	}

	@Override
	public Account get(long id) {
		Account a = null;
		Cursor c = cr.query(TdamContentProvider.ACCOUNT_CONTENT_URI,
				PROJECTION, BaseColumns._ID + "= ?",
				new String[] { String.valueOf(id) }, "1");
		if (c.moveToFirst()) {
			a = buildAccount(c);
		}
		if (!c.isClosed()) {
			c.close();
		}
		return a;
	}

	private Account buildAccount(Cursor c) {
		if (c != null) {
			final Account a = new Account();
			a.setId(c.getLong(c.getColumnIndex(Account.COL_ID)));
			a.setUsername(c.getString(c.getColumnIndex(Account.COL_USERNAME)));
			a.setPassword(c.getString(c.getColumnIndex(Account.COL_PASSWORD)));
			a.setLastSync(c.getLong(c.getColumnIndex(Account.COL_LAST_SYNC)));
			return a;
		}
		return null;
	}

	public Account get(String username, String password) {
		Account a = null;

		String customSelection = Account.COL_USERNAME + "=?" + " AND "
				+ Account.COL_PASSWORD + "=?";
		String[] customArgs = { username, password };
		Cursor c = cr.query(TdamContentProvider.ACCOUNT_CONTENT_URI,
				PROJECTION, customSelection, customArgs, "1");
		if (c.moveToFirst()) {
			a = buildAccount(c);
		}
		if (!c.isClosed()) {
			c.close();
		}
		return a;
	}

	@Override
	public List<Account> getAll() {
		List<Account> mList = Lists.newArrayList();
		Cursor c = cr.query(TdamContentProvider.ACCOUNT_CONTENT_URI,
				PROJECTION, null, null, "1");
		if (c.moveToFirst()) {
			do {
				mList.add(buildAccount(c));
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		return mList;
	}

	@Override
	public Cursor getCursor(String selection, String[] selectionArgs,
			String sortOrder) {
		return cr.query(TdamContentProvider.ACCOUNT_CONTENT_URI, PROJECTION,
				selection, selectionArgs, sortOrder);
	}

	public Account get(String user) {
		Account a = null;

		String customSelection = Account.COL_USERNAME + "=?";
		String[] customArgs = { user };
		Cursor c = cr.query(TdamContentProvider.ACCOUNT_CONTENT_URI,
				PROJECTION, customSelection, customArgs, "1");
		if (c.moveToFirst()) {
			a = buildAccount(c);
		}
		if (!c.isClosed()) {
			c.close();
		}
		return a;
	}

}
